//package com.kws.payment.controller;
//
//import cn.hutool.json.JSONUtil;
//import com.alibaba.druid.support.json.JSONUtils;
//import com.kws.common.core.domain.R;
//import com.kws.payment.dao.CreateWeixinPayNotifyRq;
//import com.kws.payment.dao.CreateWeixinPayOrderRq;
//import com.kws.payment.dao.WeixinPayRefundRq;
//import com.kws.payment.service.WeixinH5PayService;
//import com.kws.payment.service.WeixinWebPayService;
//import com.wechat.pay.contrib.apache.httpclient.auth.Verifier;
//import com.wechat.pay.java.service.refund.model.Refund;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.apache.http.entity.ContentType;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//import java.security.GeneralSecurityException;
//import java.util.HashMap;
//
///**
// * <b>WeixinPayController</b>
// *
// * @description: WeixinPayController <br>
// * @date: 2024/11/13 15:32 <br>
// * @author: lhm <br>
// * @version: 1.0 <br>
// */
//
//@Api(tags = "微信支付模块")
//@RestController
//public class WeixinPayController {
//    private static final Logger logger = LoggerFactory.getLogger(WeixinPayController.class);
//
//    @Autowired
//    private WeixinWebPayService weixinWebPayService;
//
//    @Autowired
//    private WeixinH5PayService weixinH5PayService;
//
//    @ApiOperation("web端微信支付-下单")
//    @PostMapping("/create/weixinWebPay/order")
//    public R createWeixinWebPayOrder(@RequestBody CreateWeixinPayOrderRq rq){
//        String url = weixinWebPayService.createWeixinWebPayOrder(rq);
//        return R.ok(url);
//    }
//
//    @ApiOperation("H5端微信支付-下单")
//    @PostMapping("/create/weixinH5Pay/order")
//    public R createWeixinH5PayOrder(@RequestBody CreateWeixinPayOrderRq rq){
//        String url = weixinH5PayService.createWeixinH5PayOrder(rq);
//        return R.ok(url);
//    }
//
//    @ApiOperation("web端微信支付-关闭订单")
//    @GetMapping("/close/weixinWebPay/order")
//    public R closeWeixinWebPayOrder(@RequestParam String orderNo){
//        weixinWebPayService.closeWeixinWebPayOrder(orderNo);
//        return R.ok();
//    }
//
//    @ApiOperation("H5端微信支付-关闭订单")
//    @GetMapping("/close/weixinH5Pay/order")
//    public R closeWeixinH5PayOrder(@RequestParam String orderNo){
//        weixinH5PayService.closeWeixinH5PayOrder(orderNo);
//        return R.ok();
//    }
//
//    @ApiOperation("微信支付退款申请")
//    @PostMapping("/refund/weixinPay")
//    public R weixinPayRefundOrder(@RequestBody WeixinPayRefundRq rq){
//        Refund refund = weixinWebPayService.weixinPayRefundOrder(rq);
//        return R.ok(refund);
//    }
//
//    @ApiOperation("微信支付回调通知")
//    @PostMapping("/weixinPay/notify")
//    public R weixinNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, GeneralSecurityException {
//        BufferedReader reader = request.getReader();
//        StringBuilder result = new StringBuilder();
//        String line = null;
//        while ((line = reader.readLine()) != null) {
//            if (result.length() > 0) {
//                result.append("\n");
//            }
//            result.append(line);
//        }
//        String body = result.toString();
//
//        logger.info("微信支付回调通知参数:{}"+ JSONUtil.parse(body));
//        boolean status = weixinWebPayService.weixinNotify(body);
//        if (status){
//            response.setStatus(200);
//            HashMap<Object, Object> map = new HashMap<>();
//            map.put("code", "SUCCESS");
//            map.put("message", "SUCCESS");
//            response.setHeader("Content-type", ContentType.APPLICATION_JSON.toString());
//            response.getOutputStream().write(JSONUtils.toJSONString(map).getBytes(StandardCharsets.UTF_8));
//            response.flushBuffer();
//        }
//        return R.ok();
//    }
//}
